package com.nbcio.pm.service.impl;

import com.nbcio.pm.domain.PmProject;
import com.nbcio.pm.domain.PmProjectMember;
import com.nbcio.pm.domain.PmTask;
import com.nbcio.pm.domain.bo.PmProjectMemberBo;
import com.nbcio.pm.domain.vo.PmProjectMemberVo;
import com.nbcio.pm.mapper.PmProjectMemberMapper;
import com.nbcio.pm.service.IPmProjectMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目管理验证服务
 * 用于验证项目相关的业务规则
 *
 * @author nbacheng
 */
@Service
public class PmProjectValidationService {

    @Autowired
    private IPmProjectMemberService projectMemberService;

    @Autowired
    private PmProjectMemberMapper projectMemberMapper;

    /**
     * 验证项目经理是否为项目成员
     *
     * @param projectId 项目ID
     * @param managerId 项目经理ID
     * @return 是否为有效的项目经理
     */
    public boolean validateProjectManager(Long projectId, Long managerId) {
        if (projectId == null || managerId == null) {
            return true; // 允许为空
        }

        // 查询项目成员
        PmProjectMemberBo queryBo = new PmProjectMemberBo();
        queryBo.setProjectId(projectId);
        queryBo.setUserId(String.valueOf(managerId));
        queryBo.setIsActive(true);

        List<PmProjectMemberVo> members = projectMemberService.queryList(queryBo);
        return !members.isEmpty();
    }

    /**
     * 验证任务负责人是否为项目成员
     *
     * @param projectId 项目ID
     * @param assigneeId 负责人ID
     * @return 是否为有效的负责人
     */
    public boolean validateTaskAssignee(Long projectId, Long assigneeId) {
        if (projectId == null || assigneeId == null) {
            return true; // 允许为空
        }

        // 查询项目成员
        PmProjectMemberBo queryBo = new PmProjectMemberBo();
        queryBo.setProjectId(projectId);
        queryBo.setUserId(String.valueOf(assigneeId));
        queryBo.setIsActive(true);

        List<PmProjectMemberVo> members = projectMemberService.queryList(queryBo);
        return !members.isEmpty();
    }

    /**
     * 验证任务报告人是否为项目成员
     *
     * @param projectId 项目ID
     * @param reporterId 报告人ID
     * @return 是否为有效的报告人
     */
    public boolean validateTaskReporter(Long projectId, Long reporterId) {
        if (projectId == null || reporterId == null) {
            return true; // 允许为空
        }

        // 查询项目成员
        PmProjectMemberBo queryBo = new PmProjectMemberBo();
        queryBo.setProjectId(projectId);
        queryBo.setUserId(String.valueOf(reporterId));
        queryBo.setIsActive(true);

        List<PmProjectMemberVo> members = projectMemberService.queryList(queryBo);
        return !members.isEmpty();
    }

    /**
     * 获取项目的活跃成员列表
     *
     * @param projectId 项目ID
     * @return 项目成员列表
     */
    public List<PmProjectMemberVo> getActiveProjectMembers(Long projectId) {
        if (projectId == null) {
            return List.of();
        }

        PmProjectMemberBo queryBo = new PmProjectMemberBo();
        queryBo.setProjectId(projectId);
        queryBo.setIsActive(true);

        return projectMemberService.queryList(queryBo);
    }
}
